package com.homepage.web.rest;

import com.homepage.repository.PartnerLogoRepository;
import com.homepage.service.PartnerLogoService;
import com.homepage.service.dto.PartnerLogoDTO;
import com.homepage.util.HeaderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;


/**
 * REST controller for managing {@link com.homepage.domain.PartnerLogo}.
 */
@RestController
@RequestMapping("/company/partner")
public class PartnerLogoResource {

    private final Logger log = LoggerFactory.getLogger(PartnerLogoResource.class);

    private static final String ENTITY_NAME = "partnerLogo";

    @Value("${common.clientApp.name}")
    private String applicationName;

    private final PartnerLogoService partnerLogoService;

    private final PartnerLogoRepository partnerLogoRepository;

    public PartnerLogoResource(PartnerLogoService partnerLogoService, PartnerLogoRepository partnerLogoRepository) {
        this.partnerLogoService = partnerLogoService;
        this.partnerLogoRepository = partnerLogoRepository;
    }

    @PutMapping(value = "/{id}/logo", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    @ResponseBody
    public ResponseEntity<ModelMap>  createFeaturetteImage(@PathVariable Long id,
                                                           @RequestPart MultipartFile picData) throws URISyntaxException, IOException {
        log.debug("REST request to save partner logo" );
        InputStream is = picData.getInputStream();
        String contentType = picData.getContentType();
        String fileName = picData.getOriginalFilename();
        byte[] pictureData = new byte[(int) picData.getSize()];
        int length = is.read(pictureData);
        PartnerLogoDTO partnerLogoDTO = new PartnerLogoDTO();
        partnerLogoDTO.setPartnerId(id);
        partnerLogoDTO.setLogo(pictureData);
        partnerLogoDTO.setLogoContentType(contentType);

        partnerLogoService.save(partnerLogoDTO);

        ModelMap modelMap = new ModelMap();
        modelMap.addAttribute("code", HttpStatus.CREATED.value());
        return ResponseEntity
                .created(new URI("/partner/"+id+"/logo" ))
                .headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME,  String.valueOf(id)))
                .body(modelMap);
    }


    /**
     * {@code GET  /partner-logos} : get all the partnerLogos.
     *
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of partnerLogos in body.
     */
    @GetMapping("/{id}/logo")
    public ResponseEntity<byte[]> getFeaturetteImageByFeaturatteId(@PathVariable(value = "id")  long id){
        PartnerLogoDTO partnerLogoDTO = partnerLogoService.findOneByPartnerId(id).get();
        if(partnerLogoDTO == null)
        {
            return new ResponseEntity(null,null, HttpStatus.NOT_FOUND);
        }
        HttpHeaders headers = new HttpHeaders();
        if(partnerLogoDTO.getLogoContentType().equalsIgnoreCase(MediaType.IMAGE_JPEG_VALUE))
            headers.setContentType(MediaType.IMAGE_JPEG);
        if(partnerLogoDTO.getLogoContentType().equalsIgnoreCase(MediaType.IMAGE_PNG_VALUE))
            headers.setContentType(MediaType.IMAGE_PNG);
        headers.setContentLength(partnerLogoDTO.getLogo().length);

        return new ResponseEntity<byte[]>(partnerLogoDTO.getLogo(), headers, HttpStatus.OK);
    }


    /**
     * {@code DELETE  /partner-logos/:id} : delete the "id" partnerLogo.
     *
     * @param id the id of the partnerLogoDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/{id}/logo")
    public ResponseEntity<Void> deletePartnerLogo(@PathVariable Long id) {
        log.debug("REST request to delete PartnerLogo : {}", id);
        partnerLogoService.delete(id);
        return ResponseEntity
            .noContent()
            .headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString()))
            .build();
    }
}
